package com.szewec.ps.myview;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.InputFilter;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.EditText;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.szewec.ps.R;
import com.szewec.ps.util.StringUtils;

import java.text.DecimalFormat;

public class NumInputItem extends ConstraintLayout implements BaseInput {
    protected TextView itemName;
    protected EditText value1;
    protected EditText value2;
    protected EditText value3;
    protected TextView tvShow;
    protected ConstraintLayout container;

    public NumInputItem(Context context) {
        super(context);
    }

    public NumInputItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }

    public NumInputItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        LayoutInflater.from(context).inflate(R.layout.num_input_item_view, this);
        itemName = findViewById(R.id.tv_item_name);
        value1 = findViewById(R.id.et_1);
        value2 = findViewById(R.id.et_2);
        value3 = findViewById(R.id.et_3);
        tvShow = findViewById(R.id.tv_show);
        container = findViewById(R.id.cl_container);
        value3.setFilters(new InputFilter[]{new NumberInputFilter()});
        parseStyle(context, attrs);
    }

    private void parseStyle(Context context, AttributeSet attrs) {
        if (attrs != null) {
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.InputItem);
            String name = ta.getString(R.styleable.InputItem_itemName);
            itemName.setText(name);

            boolean required = ta.getBoolean(R.styleable.InputItem_required, false);

            if (!required) {
                itemName.setCompoundDrawables(null, null, null, null);
            }

            ta.recycle();
        }
    }

    public String getStringValue(){
        String v1 = value1.getText().toString();
        String v2 = value2.getText().toString();
        String v3 = value3.getText().toString();
        if (StringUtils.isEmpty(v1) && StringUtils.isEmpty(v2) && StringUtils.isEmpty(v3) ){
            return null;
        }
        v2 = StringUtils.isEmpty(v2) ? "0" : v2;
        if (StringUtils.isEmpty(v3)){
            return v1 + "K" + v2;
        }
        if (v3.contains(".")){
            DecimalFormat df = new DecimalFormat("000.000");
            v3 = df.format(Double.valueOf(v3));
        }else {
            DecimalFormat df = new DecimalFormat("000");
            v3 = df.format(Double.valueOf(v3));
        }
        return v1 + "K" + v2 + "+" + v3;
    }


    public void setValue(String value){
        if (StringUtils.isNotEmpty(value)){
            String[] split1 = value.split("K");
            value1.setText(split1[0]);
            //如果第二个是+开头则说明第二个为空,直接设置第三个值;反之,第二个有值
            if (split1[1].startsWith("+")){
                //如果长度等于1,说明第三个也为空
                if (split1[1].length() > 1){
                    value3.setText(split1[1].substring(1));
                }
            }else {
                String[] split2 = split1[1].split("\\+");
                value2.setText(split2[0]);
                //如果length为1,则说明第三个为空
                if (split2.length > 1){
                    value3.setText(split2[1]);
                }
            }
        }

        tvShow.setText(value);
    }



    @Override
    public void switchoverMode(boolean isEnabled) {
        value1.setEnabled(isEnabled);
        value2.setEnabled(isEnabled);
        value3.setEnabled(isEnabled);
        if (isEnabled){
            container.setVisibility(VISIBLE);
            tvShow.setVisibility(GONE);
            value1.setHint(R.string.input_hint);
            value2.setHint(R.string.input_hint);
            value3.setHint(R.string.input_hint);
        }else {
            container.setVisibility(GONE);
            tvShow.setVisibility(VISIBLE);
            value1.setHint(R.string.empty_string);
            value2.setHint(R.string.empty_string);
            value3.setHint(R.string.empty_string);
        }

    }
}
